package com.example.ex5_springboot.controller;

import com.example.ex5_springboot.Result;
import com.example.ex5_springboot.pojo.AdminGetPerson;
import com.example.ex5_springboot.services.AdminGetPersonService;
import com.example.ex5_springboot.services.AdminGetPersonServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
public class AdminGetPersonController {
    @Autowired
    AdminGetPersonService adminGetPersonService;

    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        try {
            List<AdminGetPerson> allPersonInfo =adminGetPersonService.getAllPerson();
            return Result.success(allPersonInfo);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @GetMapping("/getUserByName")
    public Result getUserByName(@RequestParam("id") String Id) {
        Integer id = 0;
        try {
            id = Integer.parseInt(Id);
        } catch (NumberFormatException e) {
            return  Result.fail();
        } catch (Exception e) {
            return  Result.fail();
        }
        try {
            List<AdminGetPerson> allPersonInfo =adminGetPersonService.getPersonByName(id);
            return Result.success(allPersonInfo);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @PostMapping("/insertNewRegister")
    public Result insertNewRegister(@RequestParam("id")String id,
                                    @RequestParam("college")String college,
                                    @RequestParam("name")String name,
                                    @RequestParam("health")String health,
                                    @RequestParam("temperature")String temperature,
                                    @RequestParam("outside")String outside,
                                    @RequestParam("work")String work,
                                    @RequestParam("remark")String remark){
        Integer i = 0;
        double j = 0;
        try {
            i = Integer.parseInt(id);
            j = Double.parseDouble(temperature);
        } catch (NumberFormatException e) {
            return  Result.fail();
        } catch (Exception e) {
            return  Result.fail();
        }
        int num = adminGetPersonService.insertNewRegister(i,college,name,health,j,outside,work,remark);
        if (num != 0){
            return Result.success(num);
        } else {
            return Result.fail();
        }

    }
}
